<!-- login/index.vue - 登录页面 -->
<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-form">
        <div class="login-header">
          <div class="logo">
            <el-icon color="#1890ff" :size="32"><Shop /></el-icon>
            <span>汇流成泽电商管理后台</span>
          </div>
          <div class="tabs">
            <div class="tab active">账号密码登录</div>
            <div class="tab" @click="goToRegister">注册账号</div>
          </div>
        </div>
        
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="form">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="账号"
              prefix-icon="User"
              size="large"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="密码"
              prefix-icon="Lock"
              size="large"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <div class="remember-box">
            <el-checkbox v-model="rememberMe">7天内自动登录</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码?</el-link>
          </div>
          
          <el-button 
            type="primary" 
            :loading="loading" 
            class="login-button" 
            size="large" 
            @click="handleLogin"
          >
            登 录
          </el-button>
        </el-form>
        
        <div class="copyright">
          <div>四川省成都市小胖电商股份有限公司</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance } from 'element-plus';
import { useUserStore } from '../../store/user';

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const loginFormRef = ref<FormInstance>();
const loading = ref(false);
const rememberMe = ref(false);

const loginForm = reactive({
  username: '',
  password: ''
});

const loginRules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
};

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return;
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true;
        await userStore.loginAction(loginForm.username, loginForm.password);
        
        // 如果有重定向路径，登录后跳转到该路径，否则跳转到首页
        const redirect = route.query.redirect as string;
        router.push(redirect || '/dashboard');
      } catch (error: any) {
        ElMessage.error(error.response?.data?.error || '登录失败，请检查账号密码');
      } finally {
        loading.value = false;
      }
    }
  });
};

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register');
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  display: flex;
  position: relative;
}

.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('https://img.js.design/assets/img/659277b25c6b0e2b3e0f9fae.png') no-repeat;
  background-size: cover;
  filter: blur(2px);
  z-index: -1;
}

.login-content {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
}

.login-form {
  width: 480px;
  background: #ffffff;
  border-radius: 8px;
  padding: 40px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.login-header {
  margin-bottom: 40px;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 24px;
  font-size: 24px;
  font-weight: bold;
  color: #1890ff;
}

.logo span {
  margin-left: 12px;
}

.tabs {
  display: flex;
  border-bottom: 1px solid #e8e8e8;
}

.tab {
  padding: 12px 24px;
  cursor: pointer;
  font-size: 16px;
  color: #999;
  transition: all 0.3s;
}

.tab.active {
  color: #1890ff;
  border-bottom: 2px solid #1890ff;
}

.form {
  margin-top: 24px;
}

.remember-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.login-button {
  width: 100%;
}

.copyright {
  margin-top: 40px;
  text-align: center;
  color: #999;
  font-size: 12px;
  line-height: 1.5;
}
</style> 